package com.sunxiansheng.page;

import java.util.Collections;
import java.util.List;

import static java.util.Objects.requireNonNull;

/**
 * Description: 分页返回的实体
 * @Author sun
 * @Create 2024-07-16 13:42:50
 * @Version 1.1
 */
public class PageResult<T> {

    // 当前页码，默认为1
    private Integer pageNo = 1;

    // 每页显示的记录数，默认为20
    private Integer pageSize = 20;

    // 总记录条数
    private Integer total = 0;

    // 总页数
    private Integer totalPages = 0;

    // 当前页的记录列表
    private List<T> result = Collections.emptyList();

    // 表示当前页是从分页查询结果的第几条记录开始，下标从1开始
    private Integer start = 1;

    // 表示当前页是从分页查询结果的第几条记录结束，下标从1开始
    private Integer end = 0;

    // 私有构造函数，使用Builder创建实例
    private PageResult(Builder<T> builder) {
        this.pageNo = builder.pageNo;
        this.pageSize = builder.pageSize;
        this.total = builder.total;
        this.result = builder.result;
        calculateTotalPages();
        calculateStartAndEnd();
    }

    // Builder 模式实现
    public static class Builder<T> {
        private Integer pageNo = 1;
        private Integer pageSize = 20;
        private Integer total = 0;
        private List<T> result = Collections.emptyList();

        public Builder<T> pageNo(Integer pageNo) {
            this.pageNo = requireNonNull(pageNo, "Page number cannot be null");
            return this;
        }

        public Builder<T> pageSize(Integer pageSize) {
            this.pageSize = requireNonNull(pageSize, "Page size cannot be null");
            return this;
        }

        public Builder<T> total(Integer total) {
            this.total = requireNonNull(total, "Total count cannot be null");
            return this;
        }

        public Builder<T> result(List<T> result) {
            this.result = requireNonNull(result, "Result list cannot be null");
            return this;
        }

        public PageResult<T> build() {
            return new PageResult<>(this);
        }
    }

    // 计算总页数
    private void calculateTotalPages() {
        if (this.pageSize > 0) {
            this.totalPages = (this.total / this.pageSize) + (this.total % this.pageSize == 0 ? 0 : 1);
        } else {
            this.totalPages = 0;
        }
    }

    // 计算起始和结束位置
    private void calculateStartAndEnd() {
        if (this.pageSize > 0) {
            this.start = (this.pageNo - 1) * this.pageSize + 1;
            this.end = Math.min(this.pageNo * this.pageSize, this.total);
        } else {
            this.start = 1;
            this.end = this.total;
        }
    }

    // 获取当前页的起始位置
    public Integer getStart() {
        return start;
    }

    // 获取每页记录数
    public Integer getPageSize() {
        return pageSize;
    }

    // 获取当前页码
    public Integer getPageNo() {
        return pageNo;
    }

    // 获取总记录条数
    public Integer getTotal() {
        return total;
    }

    // 获取总页数
    public Integer getTotalPages() {
        return totalPages;
    }

    // 获取当前页的记录列表
    public List<T> getResult() {
        return result;
    }

    // 获取当前页的结束位置
    public Integer getEnd() {
        return end;
    }

    @Override
    public String toString() {
        return "PageResult{" +
                "pageNo=" + pageNo +
                ", pageSize=" + pageSize +
                ", total=" + total +
                ", totalPages=" + totalPages +
                ", result=" + result +
                ", start=" + start +
                ", end=" + end +
                '}';
    }
}
